<?php

namespace App\Repos;

use App\Models\Item;
use App\Models\ItemFee;
use App\Models\ItemFeeBox;
use App\Models\Log;
use DB;

class ItemRepo 
{
    public static function get($where=[])
    {
    	return Item::where($where)->get();
    }

    public static function find($where=[])
    {
    	return Item::where($where)->first();
    }

    public static function count($where=[])
    {
    	return Item::where($where)->count();
    }

    public static function save($data)
    {
        $rs['status'] = false;
        $item = array_only($data,['ship_name','ship_line','code','box_20','box_40','remark']);
        $flag = Item::create($item); 
        
        try {
            foreach ($data['fee_name'] as $k => $v) {
                $fee['item_id']           = $flag->id;
                $fee['fee_name']          = $v;
                $fee['receive_money']     = $data['receive_money'][$k];
                $fee['receive_contacter'] = $data['receive_contacter'][$k];
                $fee['pay_money']         = $data['pay_money'][$k];
                $fee['pay_contacter']     = $data['pay_contacter'][$k];
                $fee['fee_remark']        = $data['fee_remark'][$k];
                ItemFee::create($fee);
            }    
        } catch (\Exception $e) {
            $rs['msg'] = $e->getMessage();
            return $rs;
        }
        
        $rs['status'] = true;        
        return $rs;      
         
    }

    public static function saveFee($data)
    {
        $rs['status'] = false;
        try {
            
             ItemFee::create($data);
              
        } catch (\Exception $e) {
            $rs['msg'] = $e->getMessage();
            return $rs;
        }
        
        $rs['status'] = true;        
        return $rs;
    }

    public static function  pages($where=[])
    {
        $query = Item::query();
        if(array_get($where,'keyword')) {
            $keyword = array_pull($where,'keyword');
            $query->where('ship_name','like','%'.$keyword.'%')
                  ->orWhere('ship_line','like','%'.$keyword.'%')
                  ->orWhere('code','like','%'.$keyword.'%');
        }
        return $query->where($where)->paginate(10);
    }

    public static function delete($id)
    {
        ItemFee::where(['item_id'=>$id])->delete();
        return Item::destroy($id);
    }

    public static  function update($data,$where)
    {
        return Item::where($where)->update($data);
    }

    public static function feePages($where=[],$num=10)
    {
        $query = ItemFee::query();
       
        if(array_get($where,'start_date')) {
            $begin = array_pull($where,'start_date');
            $end   = array_pull($where,'end_date');
            $query->whereHas('item', function ($query) use ($begin,$end) {
                $query->whereBetween('created_at', [$begin,$end]);
            });
           
        }

        



        if(array_get($where,'code')) {
            $code = array_pull($where,'code');
            $query->whereHas('item', function ($query) use ($code) {
                $query->where('code', 'like', '%'.$code.'%');
            });
        }

        if(array_get($where,'ship_name')) {
            $ship_name = array_pull($where,'ship_name');
            $query->whereHas('ship_name', function ($query) use ($ship_name) {
                $query->where('ship_name', 'like', '%'.$ship_name.'%');
            });
        }

        if(array_get($where,'ship_line')) {
            $ship_line = array_pull($where,'ship_line');
            $query->whereHas('item', function ($query) use ($ship_line) {
                $query->where('ship_line', 'like', '%'.$ship_line.'%');
            });
        }

        if(array_get($where,'contacter')) {
            $contacter = array_pull($where,'contacter');
             $money_type = array_pull($where,'money_type');
             if($money_type=='receive') {
                $query->where('receive_contacter','like','%'.$contacter.'%');   
             }
             if($money_type=='pay') {
                $query->where('pay_contacter','like','%'.$contacter.'%');   
             }
            
        }
        //dd($where);
        $money_type = array_pull($where,'money_type');
        return $query->has('item')->where($where)->paginate($num);
    }

    public static function totalByType($where,$type)
    {
        $query = ItemFee::query();
       
        if(array_get($where,'start_date')) {
            $begin = array_pull($where,'start_date');
            $end   = array_pull($where,'end_date');
            $query->whereHas('item', function ($query) use ($begin,$end) {
                $query->whereBetween('created_at', [$begin,$end]);
            });
           
        }

        



        if(array_get($where,'code')) {
            $code = array_pull($where,'code');
            $query->whereHas('item', function ($query) use ($code) {
                $query->where('code', 'like', '%'.$code.'%');
            });
        }

        if(array_get($where,'ship_name')) {
            $ship_name = array_pull($where,'ship_name');
            $query->whereHas('ship_name', function ($query) use ($ship_name) {
                $query->where('ship_name', 'like', '%'.$ship_name.'%');
            });
        }

        if(array_get($where,'ship_line')) {
            $ship_line = array_pull($where,'ship_line');
            $query->whereHas('item', function ($query) use ($ship_line) {
                $query->where('ship_line', 'like', '%'.$ship_line.'%');
            });
        }

        if(array_get($where,'contacter')) {
            $contacter = array_pull($where,'contacter');
             $money_type = array_pull($where,'money_type');
             if($money_type=='receive') {
                $query->where('receive_contacter','like','%'.$contacter.'%');   
             }
             if($money_type=='pay') {
                $query->where('pay_contacter','like','%'.$contacter.'%');   
             }
        }
        $money_type = array_pull($where,'money_type');
        return $query->where($where)->has('item')->sum($type);
    }
    

    // 单项费用统计
    public static function totalByFeename($item_ids,$fee_name,$type='receive',$contacter='')
    {
        $fee_types['receive']  = 'receive_money';
        $fee_types['pay']      = 'pay_money';
        $fee_types['received'] = 'receive_money';
        $fee_types['paied']    = 'pay_money';
       
        $fee_type = $fee_types[$type];
        
        $query = ItemFee::whereIn('item_id',$item_ids)
                ->where('fee_name',$fee_name);
        if($contacter) {
            if($type=='receive') {
                $query->where('receive_contacter',$contacter);
            }

            if($type=='pay') {
                $query->where('pay_contacter',$contacter);
            }
        }        
        return  $query->sum($fee_type);
    }

    


    public static function feeUpdata($where,$data)
    {
        
        
        $log['fee_id']   = $where['id'];
        $log['remark']   = json_encode($data);
        Log::create($log);
        array_pull($data,'username');
        $flag            = ItemFee::where($where)->update($data);
        return $flag;
    }

    public static function feeDelete($id)
    {
        return ItemFee::destroy($id);
    }

    public static function feeFind($where=[])
    {
        return ItemFee::where($where)->first();
    }

}
